package com.citytransport.prokopjuk.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class Commander extends HttpServlet {

    private static final long serialVersionUID = 7141546449968805002L;
    private HashMap<String, AbstractCommand> commands = new HashMap<String, AbstractCommand>();
    static Logger logger = Logger.getLogger(Commander.class.getName());
    @Override
    public void init() throws ServletException {
	super.init();
	commands.put("/routeList.do", new RouteListServlet());
	commands.put("/login.do", new LoginServlet());
	commands.put("/admin/logout.do", new LogoutServlet());
	commands.put("/admin/addStation.do", new AddStationServlet());
	commands.put("/admin/removeStation.do", new RemoveStationServlet());
	commands.put("/admin/listStations.do", new StationListServlet());
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException {
	req.setCharacterEncoding("UTF-8");
	resp.setContentType("text/html; charset=UTF-8");
	AbstractCommand command = commands.get(req.getRequestURI());
	logger.info("call command" +req.getRequestURI() );
	if(command!= null)
	    command.processRequest(req, resp);
	else
	    resp.sendError(404);
    }
}
